package com.xn.app.muying.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.util.view.wheelview.OnWheelScrollListener;
import com.util.view.wheelview.WheelView;
import com.util.view.wheelview.adapter.NumericWheelAdapter;
import com.xn.app.muying.MainActivity;
import com.xn.app.muying.R;

import java.util.Calendar;

/**
 * Created by gang.jin on 2016/8/3.
 */
public class DateDialog {
    private static AlertDialog ad;
    static WheelView wl_start_year;
    static WheelView wl_start_month;
    static WheelView wl_start_day;
    static WheelView wl_start_hour;
    static WheelView wl_start_minute;

    public static void showShiJianRiQi(Context context, final TextView textView) {
        View popupWindowView = MainActivity.mActivity.getLayoutInflater().inflate(R.layout.ri_qi_shi_jian_dialog, null);
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(popupWindowView);
        initWheelView1(textView);
        ad.findViewById(R.id.qu_xiao_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
                textView.setText("");
            }
        });
        ad.findViewById(R.id.que_ren_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
                int n_year = wl_start_year.getCurrentItem() + 2010;//年
                int n_month = wl_start_month.getCurrentItem() + 1;//月
                int n_day = wl_start_day.getCurrentItem() + 1;//日
                int n_hore = wl_start_hour.getCurrentItem();//时
                int n_minute = wl_start_minute.getCurrentItem();//分
                textView.setText(n_year + "-" + (n_month < 10 ? "0" + n_month : n_month) + "-" + (n_day < 10 ? "0" + n_day : n_day) + " " + (n_hore < 10 ? "0" + n_hore : n_hore) + ":" + (n_minute < 10 ? "0" + n_minute : n_minute));
            }
        });
    }

    private static void initWheelView1(TextView textView) {
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);
        int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        int curDate = c.get(Calendar.DATE);
        int curHour = c.get(Calendar.HOUR_OF_DAY);
        int curMinute = c.get(Calendar.MINUTE);
//        textView.setText(curYear + "-" + (curMonth < 10 ? "0" + curMonth : curMonth) + "-" + (curDate < 10 ? "0" + curDate : curDate) + " " + (curHour < 10 ? "0" + curHour : curHour) + ":" + (curMinute < 10 ? "0" + curMinute : curMinute));
        wl_start_year = (WheelView) ad.findViewById(R.id.nian_wheelview);
        wl_start_month = (WheelView) ad.findViewById(R.id.yue_wheelview);
        wl_start_day = (WheelView) ad.findViewById(R.id.ri_wheelview);
        wl_start_hour = (WheelView) ad.findViewById(R.id.shi_wheelview);
        wl_start_minute = (WheelView) ad.findViewById(R.id.fen_wheelview);
        /*****************开始时间***********************/
        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(MainActivity.mActivity, 2010, 2050);
        numericWheelAdapterStart1.setLabel("年");
        wl_start_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(16);
        wl_start_year.setCyclic(true);//是否可循环滑动
        wl_start_year.setCurrentItem(0);

        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(MainActivity.mActivity, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_start_month.setViewAdapter(numericWheelAdapterStart2);
        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(16);
        wl_start_month.setCurrentItem(0);
        wl_start_month.setCyclic(true);

        NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(MainActivity.mActivity, 1, 31, "%02d");
        numericWheelAdapterStart3.setLabel("日");
        wl_start_day.setViewAdapter(numericWheelAdapterStart3);
        numericWheelAdapterStart3.setTextColor(R.color.black);
        numericWheelAdapterStart3.setTextSize(16);
        wl_start_day.setCyclic(true);
        wl_start_day.setCurrentItem(0);


        NumericWheelAdapter numericWheelAdapterStart4 = new NumericWheelAdapter(MainActivity.mActivity, 0, 23, "%02d");
        numericWheelAdapterStart4.setLabel("时");
        wl_start_hour.setViewAdapter(numericWheelAdapterStart4);
        numericWheelAdapterStart4.setTextColor(R.color.black);
        numericWheelAdapterStart4.setTextSize(16);
        wl_start_hour.setCyclic(true);
        wl_start_hour.setCurrentItem(0);

        NumericWheelAdapter numericWheelAdapterStart5 = new NumericWheelAdapter(MainActivity.mActivity, 0, 59, "%02d");
        numericWheelAdapterStart5.setLabel("分");
        wl_start_minute.setViewAdapter(numericWheelAdapterStart5);
        numericWheelAdapterStart5.setTextColor(R.color.black);
        numericWheelAdapterStart5.setTextSize(16);
        wl_start_minute.setCyclic(true);
        wl_start_minute.setCurrentItem(0);

        wl_start_year.setCurrentItem(curYear - 2010);
        wl_start_month.setCurrentItem(curMonth - 1);
        wl_start_day.setCurrentItem(curDate - 1);
        wl_start_hour.setCurrentItem(curHour);
        wl_start_minute.setCurrentItem(curMinute);
    }
}
